import fs from 'fs';
import childProcess from 'child_process';
import chalk from 'chalk';
export class DeleteDirOrFile {
  files = [];
  // 删除指定文件夹
  deleteDir(path, dirName) {
    let files = [];
    if (fs.existsSync(path)) {
      // 读取目录
      files = fs.readdirSync(path);
      // 遍历目录
      files.forEach((file) => {
        // 拼接文件路径
        const curPath = path + '/' + file;
        // 判断是否是文件夹
        if (fs.statSync(curPath).isDirectory()) {
          // 判断文件夹是否和dirName相等
          if (file === dirName) {
            // 删除文件夹
            childProcess.execSync(`rm -rf ${curPath}`);
            console.log(chalk.red('删除文件夹:' + curPath));
          } else {
            // 继续递归文件夹
            this.deleteDir(curPath, dirName);
          }
        }
      });
    }
  }
}

new DeleteDirOrFile().deleteDir('/Users/sairitsutakara/Desktop/student', 'node_modules');
